package jfds.interfaces;

import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

import jfds.dialogos.Cargando;
import jfds.dialogos.ControlDeCambios;
import jfds.interfaces.tablas.ModelarTabla;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class Principal extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PantallaCarga inicio;
	private Cargando Cargar;
	private ControlDeCambios Comprobando;
	
	private JPanel PanelP;
	private JScrollPane PanelScr;
	private static JTable TablaContenido;
	/**
	 * Create the frame.
	 * @param splash 
	 */
	public Principal(PantallaCarga splash) {
		this.inicio=splash;
		setProgress(0, "Iniciando ...");
		CargarComponentes();
		setProgress(20, "cargando ... cargando ...");
		setProgress(40, "Buscando Avistamientos ....");
		setProgress(60, "Preparando Escudos ...");
		setProgress(80, "Cocinando ....");
		setProgress(95, "Ya casi ....");
		setProgress(100, "Listo!!!");
		
	}
    public Principal(){
    	setTitle("JFDs version Beta");
    	setIconImage(Toolkit.getDefaultToolkit().getImage(Principal.class.getResource("/jfds/imges/iconos/f-spot.png")));
    	setLocationRelativeTo(null);
    	
    	JMenuBar menuBar = new JMenuBar();
    	setJMenuBar(menuBar);
    	
    	JMenu mnFD = new JMenu("FDS");
    	menuBar.add(mnFD);
    	
    	JMenu mnSQL = new JMenu("SQL");
    	menuBar.add(mnSQL);
    	
    	JPopupMenu popupMenu = new JPopupMenu();
    	addPopup(this, popupMenu);
    	CargarComponentes();
    }
    
    public void CargarComponentes(){
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 545, 367);
		PanelP = new JPanel();
		PanelP.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(PanelP);
		GridBagLayout gbl_PanelP = new GridBagLayout();
		gbl_PanelP.columnWidths = new int[]{83, -58, 408, 0};
		gbl_PanelP.rowHeights = new int[]{30, 35, 162, 16, 0};
		gbl_PanelP.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_PanelP.rowWeights = new double[]{0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		PanelP.setLayout(gbl_PanelP);
		
		JTree Directorios = new JTree();
		
		JPopupMenu popupDirectorio = new JPopupMenu();
		addPopup(Directorios, popupDirectorio);
		//	PanelP.add();
			GridBagConstraints gbc_Directorios = new GridBagConstraints();
			gbc_Directorios.fill = GridBagConstraints.BOTH;
			gbc_Directorios.insets = new Insets(0, 0, 5, 5);
			gbc_Directorios.gridheight = 2;
			gbc_Directorios.gridx = 0;
			gbc_Directorios.gridy = 1;
			PanelP.add(Directorios, gbc_Directorios);
		
		JToolBar toolBar = new JToolBar();
		
		JPopupMenu popuptoolbar = new JPopupMenu();
		toolBar.add(popuptoolbar);
		GridBagConstraints gbc_toolBar = new GridBagConstraints();
		gbc_toolBar.fill = GridBagConstraints.BOTH;
		gbc_toolBar.insets = new Insets(0, 0, 5, 0);
		gbc_toolBar.gridwidth = 3;
		gbc_toolBar.gridx = 0;
		gbc_toolBar.gridy = 0;
		PanelP.add(toolBar, gbc_toolBar);
		
		final JButton btnNuevo = new JButton("");
		btnNuevo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser NuevaFD = new JFileChooser();
				 FileNameExtensionFilter filtroArchivo=new FileNameExtensionFilter("FDS","fds","FDS");
				 NuevaFD.setFileFilter(filtroArchivo);
			    int seleccion = NuevaFD.showOpenDialog(btnNuevo);
			    if (seleccion == JFileChooser.APPROVE_OPTION){
			    	File ArchivoFDNuevo = NuevaFD.getSelectedFile();
			    	Comprobando = new ControlDeCambios();
			    	Comprobando.leerarchivo(ArchivoFDNuevo, 0);
			    	Comprobando.setVisible(true);
			    	Comprobando.dispose();
			    	ModelarTabla.TablaModelar(TablaContenido);
			    	PanelScr.setViewportView(TablaContenido);
			    			    	
			    }
			}
		});
		btnNuevo.setToolTipText("Agregar un nuevo Archivo FD");
		btnNuevo.setIcon(new ImageIcon(Principal.class.getResource("/jfds/imges/iconos/bullet_add.png")));
		toolBar.add(btnNuevo);
		
		JSeparator Separadormedio = new JSeparator();
		Separadormedio.setOrientation(SwingConstants.VERTICAL);
		GridBagConstraints gbc_Separadormedio = new GridBagConstraints();
		gbc_Separadormedio.fill = GridBagConstraints.HORIZONTAL;
		gbc_Separadormedio.insets = new Insets(0, 0, 5, 5);
		gbc_Separadormedio.gridheight = 2;
		gbc_Separadormedio.gridx = 1;
		gbc_Separadormedio.gridy = 1;
		PanelP.add(Separadormedio, gbc_Separadormedio);
		
		JComboBox cbRegistroP = new JComboBox();
		GridBagConstraints gbc_cbRegistroP = new GridBagConstraints();
		gbc_cbRegistroP.insets = new Insets(0, 0, 5, 0);
		gbc_cbRegistroP.gridx = 2;
		gbc_cbRegistroP.gridy = 1;
		PanelP.add(cbRegistroP, gbc_cbRegistroP);
		
		JLabel lblRegistroPrincipal = new JLabel("Registro Principal:");
		lblRegistroPrincipal.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblRegistroPrincipal = new GridBagConstraints();
		gbc_lblRegistroPrincipal.anchor = GridBagConstraints.WEST;
		gbc_lblRegistroPrincipal.insets = new Insets(0, 0, 5, 0);
		gbc_lblRegistroPrincipal.gridx = 2;
		gbc_lblRegistroPrincipal.gridy = 1;
		PanelP.add(lblRegistroPrincipal, gbc_lblRegistroPrincipal);
		
		TablaContenido = new JTable();
		TablaContenido.setBounds(130, 50, 408, 251);
		
		PanelScr = new JScrollPane(TablaContenido);
		
			
				JPopupMenu popuptabla = new JPopupMenu();
				addPopup(TablaContenido, popuptabla);
				GridBagConstraints gbc_PanelScr = new GridBagConstraints();
				gbc_PanelScr.fill = GridBagConstraints.BOTH;
				gbc_PanelScr.insets = new Insets(0, 0, 5, 0);
				gbc_PanelScr.gridx = 2;
				gbc_PanelScr.gridy = 2;
				PanelP.add(PanelScr, gbc_PanelScr);
		
		JProgressBar progressBar = new JProgressBar();
		GridBagConstraints gbc_progressBar = new GridBagConstraints();
		gbc_progressBar.fill = GridBagConstraints.BOTH;
		gbc_progressBar.gridwidth = 3;
		gbc_progressBar.gridx = 0;
		gbc_progressBar.gridy = 3;
		PanelP.add(progressBar, gbc_progressBar);
		setLocationRelativeTo(null);
    }
    
    public void setProgress(int Porciento, String leyenda){
    	inicio.getProgreso().setValue(Porciento);
    	inicio.getLblCarga().setText(leyenda);
    	try {
			Thread.sleep(30);
		} catch (InterruptedException e) {
			JOptionPane.showMessageDialog(null, "Error Al Cargar la Aplicacion");
		}
    	
    }
    public void setCargar(int Cargado){
    	Cargar.getCargandoBar().setValue(Cargado);
    	
    }
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	
}
